package drone.basis.util ;

import java.net.URLEncoder ;
import java.util.Date ;

import com.alibaba.fastjson.JSONObject ;
import com.aliyuncs.CommonRequest ;
import com.aliyuncs.CommonResponse ;
import com.aliyuncs.DefaultAcsClient ;
import com.aliyuncs.IAcsClient ;
import com.aliyuncs.http.MethodType ;
import com.aliyuncs.profile.DefaultProfile ;

import drone.basis.GlobalConfig ;

public class SmsUtil
{

	// 同号码发送间隔时长毫秒
	private static Long distance = GlobalConfig.MINUTE * 30 ;

	private static String sysDomain = "dysmsapi.aliyuncs.com" ;

	private static String sysActionSend = "SendSms" ;

	private static String sysActionQuery = "QuerySendDetails" ;

	private static String sysVersion = "2017-05-25" ;

	private static String regionId = "cn-hangzhou" ;

	private static String accessKeyId = "LTAI4G7netvrXEBvF3WPM7vw" ;

	private static String accessSecret = "4YiZEGCZle1jpYJC8PIupm7HGyHAYO" ;

	private static IAcsClient client = new DefaultAcsClient ( DefaultProfile.getProfile ( regionId , accessKeyId , accessSecret ) ) ;

	// 设备报警短信
	public static Boolean sendSms (
			String signName ,
			String smsTemplate ,
			String smsPhone ,
			JSONObject smsParam
	)
	{

		// if ( querySms ( smsPhone ) )
		// {
		// return ;
		// }

		if ( StringUtil.isEmpty ( smsPhone ) )
		{
			return false ;
		}

		if ( StringUtil.isEmpty ( smsTemplate ) )
		{
			return false ;
		}

		if ( StringUtil.isEmpty ( signName ) )
		{
			signName = "ZLSZGS" ;
		}

		CommonRequest request = new CommonRequest ( ) ;
		request.setSysMethod ( MethodType.POST ) ;
		request.setSysDomain ( sysDomain ) ;
		request.setSysVersion ( sysVersion ) ;
		request.setSysAction ( sysActionSend ) ;
		request.putQueryParameter ( "RegionId" , regionId ) ;
		request.putQueryParameter ( "PhoneNumbers" , smsPhone ) ;
		request.putQueryParameter ( "SignName" , signName ) ;
		request.putQueryParameter ( "TemplateCode" , smsTemplate ) ;

		request.putQueryParameter ( "TemplateParam" , smsParam.toJSONString ( ) ) ;
		try
		{
			CommonResponse response = client.getCommonResponse ( request ) ;
			JSONObject resultObject = JSONObject.parseObject ( response.getData ( ) ) ;
			if ( resultObject.containsKey ( "Code" ) && resultObject.getString ( "Code" ).equals ( "OK" ) )
			{
				System.out.println ( "发送成功" ) ;
				return true ;
			}
		} catch ( Exception e )
		{
			e.printStackTrace ( ) ;
			return false ;
		}

		System.out.println ( "发送失败 : " + signName + " " + smsPhone + " " + smsTemplate + " " + smsParam ) ;
		return false ;
	}

	public static Boolean querySms (
			String smsPhone
	)
	{

		CommonRequest request = new CommonRequest ( ) ;
		request.setSysMethod ( MethodType.POST ) ;
		request.setSysDomain ( sysDomain ) ;
		request.setSysVersion ( sysVersion ) ;
		request.setSysAction ( sysActionQuery ) ;
		request.putQueryParameter ( "RegionId" , regionId ) ;
		request.putQueryParameter ( "PhoneNumber" , smsPhone ) ;
		request.putQueryParameter ( "SendDate" , DateUtil.format ( new Date ( ) , DateUtil.DATENOSPACE ) ) ;
		request.putQueryParameter ( "PageSize" , "1" ) ;
		request.putQueryParameter ( "CurrentPage" , "1" ) ;

		try
		{
			CommonResponse response = client.getCommonResponse ( request ) ;
			JSONObject resultObject = JSONObject.parseObject ( response.getData ( ) ) ;
			if ( resultObject.containsKey ( "Code" ) && resultObject.getString ( "Code" ).equals ( "OK" ) )
			{
				if ( resultObject.getIntValue ( "TotalCount" ) > 0 )
				{
					JSONObject object = resultObject.getJSONObject ( "SmsSendDetailDTOs" ).getJSONArray ( "SmsSendDetailDTO" ).getJSONObject ( 0 ) ;
					if ( object.containsKey ( "SendDate" ) )
					{
						Date oldDate = object.getDate ( "SendDate" ) ;
						Date newDate = new Date ( ) ;
						if ( DateUtil.distance ( oldDate , newDate ) < distance )
						{
							System.out.println ( "同手机号发送间隔不能低于" + distance / GlobalConfig.MINUTE + "分钟" ) ;
							return true ;
						}
					}
				}
			}
			return false ;
		} catch ( Exception e )
		{
			e.printStackTrace ( ) ;
			return false ;
		}

	}

	public static String sendSmsQihe (
			String smsPhone ,
			String smsContent
	) throws Exception
	{

		String account = "800090" ; // 账号
		String password = "123456" ; // 密码
		String extno = "1069010" ; // 接入码
		String url = "http://47.98.43.115:7862/sms" ; // 接口地址

		smsContent = "【齐河齐源水务集团有限公司】" + smsContent ;
		smsContent = URLEncoder.encode ( smsContent , "UTF-8" ) ;
		String sendUrl = url + "?action=send&account=" + account + "&password=" + password + "&mobile=" + smsPhone + "&content=" + smsContent + "&extno=" + extno + "&rt=json" ;

		String res = HttpUtil.post ( sendUrl ) ;
		return res ;
	}

	public static void main (
			String [ ] args
	) throws Exception
	{

		// querySms( "15063337733");

//		String satId = "01100005" ;
//		String satBalance = "11.05" ;
//		String satAddr = "二楼101" ;
//		String satName = "测试" ;
//		String txnName = "2021-07" ;
//		String txnMin = "310" ;
//		String txnMax = "14" ;
//		String txnCount = "25.0" ;
//		String txnAmount = "354.01" ;
//		String payLimit = "15" ;
//		String satLimit = "30" ;
//		String repTime = "早上8点到晚上12点" ;
//
//		String hurry = "尊敬的用户您好：给水号[satId]的用户[satName]，地址[satAddr]。[txnName]用水量[txnCount]方，欠费金额[txnAmount]元，请于[payLimit]日前及时缴纳，以免影响您的正常用水" ;
//		String asset = "尊敬的用户您好：给水号[satId]的用户[satName]，地址[satAddr]。[txnName]抄表起码[txnMin]，止码[txnMax]，用水量[txnCount]方，金[txnAmount]元，本期账单已结清，当前账户余额为[satBalance]元" ;
//		String debts = "尊敬的用户您好：给水号[satId]的用户[satName]，地址[satAddr]。[txnName]抄表起码[txnMin]，止码[txnMax]，用水量[txnCount]方，金额[txnAmount]元，当前账户余额为[satBalance]元，请及时缴纳。（如已缴费请忽略）" ;
//		String exceed = "尊敬的用户您好：您的账户余额已经低于[satLimit]元。请注意及时缴存水费，以免欠费引起阀门自动关闭，影响您的正常用水。" ;
//		String repair = "尊敬的用户您好：因管道抢修，现已停水。大约$[repTime]左右恢复正常供水。给您带来的不便敬请谅解。" ;
//		String alert = "尊敬的用户您好：寒潮天气到来，容易导致居民用水设施损坏，请提前做好水表、水管等用水设施防冻保暖工作，避免室内外用水设施冻裂，给您的生活带来不便。" ;
//		String notice = "尊敬的用户您好：因系统升级，现正在调试中，给您带来的不便，敬请谅解！" ;
//
//		// String
//		// hurry="尊敬的用水户([satName]):您的缴费号码[satId],本月实用水方[txnCount],合计金额[txnAmount],请您本月15日前缴清水费,否则予以停水处理,缴清水费后24小时内给与通水,敬请关注定陶区自来水公司公众号";
//
//		String hurryMessage = hurry.replace ( "[satId]" , satId ).replace ( "[satName]" , satName ).replace ( "[satAddr]" , satAddr ).replace ( "[txnName]" , txnName ).replace ( "[txnCount]" , txnCount )
//				.replace ( "[txnAmount]" , txnAmount ).replace ( "[payLimit]" , payLimit ) ;
//
//		String assetMessage = asset.replace ( "[satId]" , satId ).replace ( "[satName]" , satName ).replace ( "[satAddr]" , satAddr ).replace ( "[txnName]" , txnName ).replace ( "[txnMin]" , txnMin ).replace ( "[txnMax]" , txnMax )
//				.replace ( "[txnCount]" , txnCount ).replace ( "[txnAmount]" , txnAmount ).replace ( "[satBalance]" , satBalance ) ;
//
//		String debtsMessage = debts.replace ( "[satId]" , satId ).replace ( "[satName]" , satName ).replace ( "[satAddr]" , satAddr ).replace ( "[txnName]" , txnName ).replace ( "[txnMin]" , txnMin ).replace ( "[txnMax]" , txnMax )
//				.replace ( "[txnCount]" , txnCount ).replace ( "[txnAmount]" , txnAmount ).replace ( "[satBalance]" , satBalance ) ;
//
//		String exceedMessage = exceed.replace ( "[satLimit]" , satLimit ) ;
//
//		String repairMessage = repair.replace ( "[repTime]" , repTime ) ;
//
//		String alertMessage = alert ;
//
//		String noticeMessage = notice ;
//
//		JSONObject templateParam = new JSONObject ( ) ;
//		templateParam.put ( "msg" , noticeMessage ) ;
//		templateParam.put ( "msg" , alertMessage ) ;
//		templateParam.put ( "msg" , repairMessage ) ;
//		templateParam.put ( "msg" , exceedMessage ) ;
//		templateParam.put ( "msg" , debtsMessage ) ;
//		templateParam.put ( "msg" , assetMessage ) ;
//		templateParam.put ( "msg" , hurryMessage ) ;
		
		JSONObject tem=new JSONObject();
		tem.put("msg","760915");

		sendSms ( "金思泉水务" , "SMS_236450493" , "15684793508" , tem ) ;

	}

}
